package com.lczc.soa.controller;

import com.lczc.soa.domain.Area;
import com.lczc.soa.domain.SearchAreaResponse;
import com.lczc.soa.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/Area")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @RequestMapping("/toadd")
    public String addAreaPage(Model model){
        model.addAttribute("area",new Area());
        return "area_add";
    }

    @RequestMapping("/add")
    public String addArea(Area area){
//        System.out.println("Controller:" + area.getAreaName());
        int updated = areaService.saveArea(area);
        return "redirect:/Area/list";
    }

    @RequestMapping("/list")
    public String AreaList(){
        return "area_list";
    }

    @GetMapping("/api/search")
    @ResponseBody
    public SearchAreaResponse searchAreaResponse(@RequestParam("pageNum") Integer pageNum,
                                                 @RequestParam("pageSize") Integer pageSize){
        return areaService.findAllArea(pageNum,pageSize);
    }

    @GetMapping("/api/searchLike")
    @ResponseBody
    public SearchAreaResponse searchAreaResponseLike(@RequestParam("areaName") String areaName,
                                                     @RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize){
        return areaService.findAreaLikeName(areaName,pageNum,pageSize);
    }

    @RequestMapping("/edit")
    public String EditArea(@RequestParam("id") String id, Model model){
        Area area = areaService.findAreaByPrimaryKey(id);
        model.addAttribute("area",area);
        return "area_add";
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteArea(@RequestParam("id") String id){
        int deleted = areaService.deleteArea(id);
        return deleted > 0;
    }

    @RequestMapping("/toDistrictMap1")
    public String toMap1(){
        return "district_map1";
    }

    @RequestMapping("/toDistrictMap2")
    public String toMap2(){
        return "district_map2";
    }

    @RequestMapping("/toDistrictMap3")
    public String toMap3(){
        return "district_map3";
    }

}
